#a basic inventory system.
class Inventory:
	#initialisation function
	def __init__(self):
		#todo load the dictionary of known items
		itemsfile = open("knownitems.tsv", "r"); #open the knownitems file
		items = itemsfile.readlines(); #read all the lines in the file into a list
		
		self.knownitems = {}; #create dictionary to store the items
		
		#loop over all the lines in the file
		for item in items:
			itemprops = item.split("\t"); #split the line into a list of item properties
			print(itemprops);
			#loop over all the item's properties
			i = 0; #remember which iteration we are on
			for itemprop in itemprops:
				if i == 0: #the name of the item
					itemname = itemprop.strip(); #store the item's name temporarily, stripping any potential whitespace
					self.knownitems[itemname] = {}; #create a dictionary to store it's properties
				if i == 1: #the value of the item
					self.knownitems[itemname]["value"] = itemprop.strip(); #strip any whitespace from the ends
				if i == 2: #the maximum quantity of the item
					self.knownitems[itemname]["maxqty"] = itemprop.strip();
				if i == 1: #the description of the item
					self.knownitems[itemname]["description"] = itemprop.strip();
				i += 1;
		
		itemsfile.close(); #close the known items file again
		
		#create a dictionary to store the inventory
		self.contents = {};
	
	'''
	function to add an item to the inventory. Parameter descriptions:
	
		itemname	| the name of the item to add (this will be used for display).
		qty = 1		| the quantity of the item to add.
	
	'''
	def add(self, itemname, qty = 1):
		#check to see if the item already exists in the inventory
		if(self.contents.get(itemname)):
			#the item does exist in the inventory, add to it - but only if there is room.
			if(self.contents[itemname]["qty"] + qty > self.contents[itemname]["maxqty"]):
				#add the items would go above the limit, don't add anything
				#question do we want to add as many as we can fit here?
				return False;
			
			#adding the items will not make the inventory overflow, go ahead and add them
			self.contents[itemname]["qty"] += qty;
		else:
			#the item does not exist in the inventory, add it!
			self.contents[itemname] = {
				"name"	: itemname,
				"qty"	: qty,
				"maxqty": self.knownitems[itemname]["maxqty"]
			};
			#todo check to see if the item actually exists in the knownitems database
			self.contents[itemname].update(self.knownitems[itemname]); #add the maxqty and the description from the knownitems database to the item in the inventory
		
		return self.contents[itemname]["qty"]; #return the new quantity of the item that specified for convenience
	
	#function to get and return in information stored about itemname from the inventory.
	#returns False if itemname is not stored in the inventory.
	def get(self, itemname):
		if(self.contents.get(itemname)):
			#the item is in the inventory
			return self.contents[itemname];
		else:
			#the item isn't in the inventory
			return False;
	
	#function to remove qty items of itemname from the inventory. qty can be either a number or the special keyword all.
	#returns True when there are no more of the item left, otherwise returns False.
	def remove(self, itemname, qty = 1):
		#todo remove an item from the inventory here...!
		if(self.contents.get(itemname)):
			#the item is in the inventory
			if(qty == "all"):
				#the special keyword "all" was used, remove all the items
				self.contents.pop(itemname);
				return True;
			else:
				#remove the items normally
				self.contents[itemname]["qty"] -= qty;
				if(self.contents[itemname]["qty"] <= 0):
					#there are no items left, remove the entry
					self.contents.pop(itemname);
					return True;
		
		return False;
	
	#function to check to see if the inventory as itemname stored
	def has(self, itemname):
		if(self.contents.get(itemname)):
			return True; #the item is in the inventory
		else:
			return False; #the item isn't in the inventory
